#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <arpa/inet.h>
#include <ctype.h>
#include "stdShared.h"
using namespace std;
#include <pthread.h>
#include <json-c/json.h>

#define PORT 8888
#define Size 64

struct UserInfo
{
    UserInfo() {}
    UserInfo(char *name, char *passwd, int flag)
    {
        strcpy(m_name, name);
        strcpy(m_passwd, passwd);
        m_flag = flag;
    }
    char m_name[Size];
    char m_passwd[Size];
    int m_flag;
};

void * servers(void * arg)
{
    pthread_detach(pthread_self());
    int connfd = *(int *)arg;
    UserInfo user;
    char buffer[Size] = {0};
    int length = 0;

    while (1)
    {
        int readBytes = read(connfd,&length,sizeof(int));
        if(readBytes > 0)
        {
            char *str = (char * )malloc(sizeof(char)* (length + 1));
            readBytes = read( connfd,str, sizeof(char)* (length + 1));
            if(readBytes > 0)
            {
                //cout << "buffer : " << buffer << endl;

                json_object * jsonObj = json_tokener_parse(str);
                if(jsonObj == NULL)
                {
                    cout << "解析失败" << endl;
                }
                else
                {
                    /* 解析成功 */
                    int type = json_object_get_int(json_object_object_get(jsonObj,"type"));
                    const char * username = json_object_get_string(json_object_object_get(jsonObj,"username"));
                    const char * passwd = json_object_get_string(json_object_object_get(jsonObj,"passwd"));
                    cout << "type : " << type << ", username : " << username << ", passwd : " << passwd << endl;
                }
                
            }   
            else if(readBytes == 0)
            {
                cout << "客户端下线" << endl;
                break;
            }
            else
            {
                cout << "readBytes < 0" << endl;
            }
        }
        sleep(60);
    }
    close(connfd);
    pthread_exit(NULL);
}

int main()
{
    // 创建通信文件描述符
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket create error:");
        _exit(-1);
    }
    /* 设置地址 */
    int optVal = 1;
    /* 端口复用 多个进程监听同一个端口*/
    int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(optVal));
    if (ret == -1)
    {
        perror("setsockopt error");
        exit(1);
    }
    // 绑定服务器的IP和端口
    sockaddr_in localAddress;
    // 清除脏数据
    memset(&localAddress, 0, sizeof(localAddress));
    // 地址族
    localAddress.sin_family = AF_INET;         // ipv4
    localAddress.sin_port = htons(PORT);       // 端口
    localAddress.sin_addr.s_addr = INADDR_ANY; // 设置IP地址 INADDR_ANY:通常为0.0.0.0，代表绑定到所有可用的网络接口 addr(地址)

    // 绑定
    ret = bind(sockfd, (const sockaddr *)&localAddress, sizeof(localAddress));
    if (ret == -1)
    {
        perror("bind error:");
        _exit(-1);
    }

    // 监听
    ret = listen(sockfd, 10);
    if (ret == -1)
    {
        perror("listen error:");
        _exit(-1);
    }
    cout << "等待客户端连接....." << endl;

    while(1)
    {
        // 阻塞接受客户端的连接
        int connfd = accept(sockfd, NULL, NULL);
        if (connfd == -1)
        {
            perror("accept error");
            _exit(-1);
        }
        cout << "connfd: " << connfd << endl;

        /* 创建线程 */
        pthread_t tid;
        ret = pthread_create(&tid,NULL,servers,(void *)&connfd);
        if(ret != 0)
        {
            perror("thread create error!");
            exit(1);
        }
        /* 睡眠1s 让线程读取数据*/
        sleep(1);
       
    }
    //close(connfd);
    close(sockfd);

    return 0;
}